import 'package:flutter/material.dart';
import 'package:kxy_flutter_app/r.dart';
import 'package:kxy_flutter_app/view/my/basic_info/break_rule_list_view.dart';
import 'package:kxy_flutter_app/view/root/view_model/navigation_model.dart';
import 'package:provider/provider.dart';
import 'cert_list_view.dart';
import 'user_info_editor_view.dart';
import 'work_history_list_view.dart';

class UserInfoView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text('基本信息'),
      ),
      body: ListView(
        children: ListTile.divideTiles(
          color: Colors.black12,
          tiles: [
            _createActionRow(
              context,
              R.assetsImgMyInfo,
              '基本信息',
              view: UserInfoEditorView(),
            ),
            Consumer<NavigationModel>(
              builder: (cxt, vm, child) {
                return _createActionRow(
                  context,
                  R.assetsImgMyCert,
                  '证件管理',
                  view: CertListView(),
                  withNotice: vm.certCount > 0,
                );
              },
            ),
            _createActionRow(
              context,
              R.assetsImgMySwRecord,
              '违章记录',
              view: BreakRuleListView(),
            ),
            _createActionRow(
              context,
              R.assetsImgMyWorkHistory,
              '工作经历',
              view: WorkHistoryListView(),
            ),
          ],
        ).toList(),
      ),
    );
  }

  Widget _createActionRow(BuildContext context, String imgName, String title, {Widget view, bool withNotice = false}) {
    return ListTile(
      dense: true,
      leading: _createIcon(imgName),
      title: _createTitle(title, withNotice),
      trailing: Icon(
        Icons.chevron_right,
        size: 24,
      ),
      onTap: () {
        Navigator.of(context).push(
          MaterialPageRoute(
            builder: (cxt) => view ?? UserInfoEditorView(),
          ),
        );
      },
    );
  }

  Widget _createTitle(String title, bool withNotice) {
    if (withNotice == false) {
      return Text(
        title,
        style: TextStyle(
          fontSize: 14,
          color: Colors.black,
        ),
      );
    }

    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Text(
          title,
          style: TextStyle(
            fontSize: 14,
            color: Colors.black,
          ),
        ),
        Container(
          height: 7,
          width: 7,
          margin: EdgeInsets.only(left: 5),
          decoration: BoxDecoration(
            color: Colors.red,
            shape: BoxShape.circle,
          ),
        ),
      ],
    );
  }

  Widget _createIcon(String imgName) {
    return Image(
      image: AssetImage(imgName),
      width: 24,
      height: 24,
    );
  }
}
